#!/bin/bash
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019-2026 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Emulator for NVIDIA Jetson nvpmodel

usage()
{
    if [ "$1" != "" ]; then
        tput setaf 1
        echo "$1"
        tput sgr0
    fi
    
    echo "EMULATOR Nvidia Power Model Tool Version 1.1.2"
    echo "Usage:"
    echo "$0 [options]"
    
cat << EOF
    nvpmodel [-h | --help] [--verbose] [-q | --query] [-p | --parse] [-u | --udata]
        [-w | --wait <sec>] [--boot] [-m | --mode <mode>] [-f | --conf <filename>] [-o | --os <android,l4t>]
-h, --help:
    Print this help info.
--verbose:
    Enable verbose log.
--boot:
    Exit and do nothing if it is sanity-flashed build.
-p, --parse:
    Parse the config file only. Recommended to enable verbose log.
-m, --mode <mode>:
    <mode> is one of the integer POWER_MODEL ID defined in config file. Switch to the specified power mode.
-f, --conf:
    explicitly specify the config file.
    If it is the only option, then it sets the power mode as default mode configured in the file.
    This option can be used for developer usage to specify a config file other than the default one.
-o, --os <android,l4t>:
    Perform OS specific operations for power model settings. Argument is case insensitive.
-q, --query:
    Query the current power mode.
-w, --wait:
    delay exectuion by specified amount of seconds.
-u, --udata:
    specify the absolute path for user data file when set or query power mode.
Examples:
    nvpmodel -m 2: switch to POWER_MODEL ID=2 of which settings are defined in the default configuration file.
    nvpmodel -m 2 -o android: switch to POWER_MODEL ID=2 and perform Android specific operations for power mode.
    nvpmodel -m 2 -f pm.conf: switch to POWER_MODEL ID=2 of which settings are defined in pm.conf.
    nvpmodel -m 2 -u /data/status: switch to POWER_MODEL ID=2 and store the active mode as user settings in /data/status.
    nvpmodel -f pm.conf: read the active mode in user data file and set it as the power mode which is configured in pm.conf.
        If user data file does not exist or the active mode value is invalid, set defalut mode instead.
    nvpmodel -q: print the current power mode.
    nvpmodel -q --verbose: print the current power mode with verbose info.
    nvpmodel -p -f pm.conf: parse pm.conf and print the result.
EOF
}

show_verbose_nano()
{
cat << EOF
NVPM VERB: PM_CONFIG: DEFAULT=MAXN(0)
NVPM VERB: PARAM TYPE=FILE NAME=CPU_ONLINE
NVPM VERB: CORE_0 /sys/devices/system/cpu/cpu0/online
NVPM VERB: CORE_1 /sys/devices/system/cpu/cpu1/online
NVPM VERB: CORE_2 /sys/devices/system/cpu/cpu2/online
NVPM VERB: CORE_3 /sys/devices/system/cpu/cpu3/online
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_ENABLE
NVPM VERB: GPU_PWR_CNTL_EN /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_DISABLE
NVPM VERB: GPU_PWR_CNTL_DIS /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_A57
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=GPU
NVPM VERB: FREQ_TABLE /sys/devices/gpu.0/devfreq/57000000.gpu/available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/gpu.0/devfreq/57000000.gpu/max_freq
NVPM VERB: MIN_FREQ /sys/devices/gpu.0/devfreq/57000000.gpu/min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=EMC
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/emc_iso_cap
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=0 NAME=MAXN
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_A57 MIN_FREQ 0
NVPM VERB: CPU_A57 MAX_FREQ 2147483647
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 2147483647
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 0
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=1 NAME=5W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 0
NVPM VERB: CPU_ONLINE CORE_3 0
NVPM VERB: CPU_A57 MIN_FREQ 0
NVPM VERB: CPU_A57 MAX_FREQ 918000
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 640000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1600000000
NVPM VERB:
EOF
}

show_verbose_xavier()
{
cat << EOF
NVPM VERB: PM_CONFIG: DEFAULT=MODE_15W(2)
NVPM VERB: PARAM TYPE=FILE NAME=CPU_ONLINE
NVPM VERB: CORE_0 /sys/devices/system/cpu/cpu0/online
NVPM VERB: CORE_1 /sys/devices/system/cpu/cpu1/online
NVPM VERB: CORE_2 /sys/devices/system/cpu/cpu2/online
NVPM VERB: CORE_3 /sys/devices/system/cpu/cpu3/online
NVPM VERB: CORE_4 /sys/devices/system/cpu/cpu4/online
NVPM VERB: CORE_5 /sys/devices/system/cpu/cpu5/online
NVPM VERB: CORE_6 /sys/devices/system/cpu/cpu6/online
NVPM VERB: CORE_7 /sys/devices/system/cpu/cpu7/online
NVPM VERB: PARAM TYPE=FILE NAME=TPC_POWER_GATING
NVPM VERB: TPC_PG_MASK /sys/devices/gpu.0/tpc_pg_mask
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_ENABLE
NVPM VERB: GPU_PWR_CNTL_EN /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_DISABLE
NVPM VERB: GPU_PWR_CNTL_DIS /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_DENVER_0
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_DENVER_1
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu2/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu2/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_DENVER_2
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu4/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_DENVER_3
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu6/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu6/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=GPU
NVPM VERB: FREQ_TABLE /sys/devices/17000000.gv11b/devfreq/17000000.gv11b/available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/17000000.gv11b/devfreq/17000000.gv11b/max_freq
NVPM VERB: MIN_FREQ /sys/devices/17000000.gv11b/devfreq/17000000.gv11b/min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=EMC
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/emc_iso_cap
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA_CORE
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_dla
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA_FALCON
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_dla_falcon
NVPM VERB: PARAM TYPE=CLOCK NAME=PVA_VPS
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_pva_vps
NVPM VERB: PARAM TYPE=CLOCK NAME=PVA_CORE
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_pva_core
NVPM VERB: PARAM TYPE=CLOCK NAME=CVNAS
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_cvnas
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=0 NAME=MAXN
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 1
NVPM VERB: CPU_ONLINE CORE_7 1
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 2147483647
NVPM VERB: CPU_DENVER_1 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MAX_FREQ 2147483647
NVPM VERB: CPU_DENVER_2 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_2 MAX_FREQ 2147483647
NVPM VERB: CPU_DENVER_3 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_3 MAX_FREQ 2147483647
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 2147483647
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 0
NVPM VERB: DLA_CORE MAX_FREQ 2147483647
NVPM VERB: DLA_FALCON MAX_FREQ 2147483647
NVPM VERB: PVA_VPS MAX_FREQ 2147483647
NVPM VERB: PVA_CORE MAX_FREQ 2147483647
NVPM VERB: CVNAS MAX_FREQ 2147483647
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=1 NAME=MODE_10W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 0
NVPM VERB: CPU_ONLINE CORE_3 0
NVPM VERB: CPU_ONLINE CORE_4 0
NVPM VERB: CPU_ONLINE CORE_5 0
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 5
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 1200000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 520000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1065600000
NVPM VERB: DLA_CORE MAX_FREQ 550000000
NVPM VERB: DLA_FALCON MAX_FREQ 330000000
NVPM VERB: PVA_VPS MAX_FREQ 115200000
NVPM VERB: PVA_CORE MAX_FREQ 115200000
NVPM VERB: CVNAS MAX_FREQ 601600000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=2 NAME=MODE_15W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 0
NVPM VERB: CPU_ONLINE CORE_5 0
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MAX_FREQ 1200000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 670000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1331200000
NVPM VERB: DLA_CORE MAX_FREQ 750000000
NVPM VERB: DLA_FALCON MAX_FREQ 450000000
NVPM VERB: PVA_VPS MAX_FREQ 550000000
NVPM VERB: PVA_CORE MAX_FREQ 385000000
NVPM VERB: CVNAS MAX_FREQ 716800000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=3 NAME=MODE_30W_ALL
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 1
NVPM VERB: CPU_ONLINE CORE_7 1
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MAX_FREQ 1200000
NVPM VERB: CPU_DENVER_2 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_2 MAX_FREQ 1200000
NVPM VERB: CPU_DENVER_3 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_3 MAX_FREQ 1200000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 900000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1600000000
NVPM VERB: DLA_CORE MAX_FREQ 1050000000
NVPM VERB: DLA_FALCON MAX_FREQ 630000000
NVPM VERB: PVA_VPS MAX_FREQ 760000000
NVPM VERB: PVA_CORE MAX_FREQ 532000000
NVPM VERB: CVNAS MAX_FREQ 1011200000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=4 NAME=MODE_30W_6CORE
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 1450000
NVPM VERB: CPU_DENVER_1 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MAX_FREQ 1450000
NVPM VERB: CPU_DENVER_2 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_2 MAX_FREQ 1450000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 900000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1600000000
NVPM VERB: DLA_CORE MAX_FREQ 1050000000
NVPM VERB: DLA_FALCON MAX_FREQ 630000000
NVPM VERB: PVA_VPS MAX_FREQ 760000000
NVPM VERB: PVA_CORE MAX_FREQ 532000000
NVPM VERB: CVNAS MAX_FREQ 1011200000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=5 NAME=MODE_30W_4CORE
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 0
NVPM VERB: CPU_ONLINE CORE_5 0
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 1780000
NVPM VERB: CPU_DENVER_1 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_1 MAX_FREQ 1780000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 900000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1600000000
NVPM VERB: DLA_CORE MAX_FREQ 1050000000
NVPM VERB: DLA_FALCON MAX_FREQ 630000000
NVPM VERB: PVA_VPS MAX_FREQ 760000000
NVPM VERB: PVA_CORE MAX_FREQ 532000000
NVPM VERB: CVNAS MAX_FREQ 1011200000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=6 NAME=MODE_30W_2CORE
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 0
NVPM VERB: CPU_ONLINE CORE_3 0
NVPM VERB: CPU_ONLINE CORE_4 0
NVPM VERB: CPU_ONLINE CORE_5 0
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_DENVER_0 MIN_FREQ 1200000
NVPM VERB: CPU_DENVER_0 MAX_FREQ 2100000
NVPM VERB: GPU MIN_FREQ 318750000
NVPM VERB: GPU MAX_FREQ 900000000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 1600000000
NVPM VERB: DLA_CORE MAX_FREQ 1050000000
NVPM VERB: DLA_FALCON MAX_FREQ 630000000
NVPM VERB: PVA_VPS MAX_FREQ 760000000
NVPM VERB: PVA_CORE MAX_FREQ 532000000
NVPM VERB: CVNAS MAX_FREQ 1011200000
NVPM VERB:
EOF
}

show_verbose_test()
{
cat << EOF
NVPM VERB: Config file: /etc/nvpmodel.conf
NVPM VERB: parsing done for /etc/nvpmodel.conf
succeed to parse file /etc/nvpmodel.conf.
NVPM VERB: PM_CONFIG: DEFAULT=MIN_MAX_TEST(3)
NVPM VERB: PARAM TYPE=CLOCK NAME=CVNAS
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/nafll_cvnas
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=0 NAME=MAXTEST
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=1 NAME=TEST
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=2 NAME=MINTEST
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 3
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=3 NAME=MIN_MAX_TEST
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: CPU_ONLINE CORE_0 1
EOF
}

show_verbose_orin()
{
cat << EOF
NVPM VERB: Config file: /etc/nvpmodel.conf
NVPM VERB: parsing done for /etc/nvpmodel.conf
succeed to parse file /etc/nvpmodel.conf.
NVPM VERB: PM_CONFIG: DEFAULT=MODE_30W(2)
NVPM VERB: PARAM TYPE=FILE NAME=CPU_ONLINE
NVPM VERB: CORE_0 /sys/devices/system/cpu/cpu0/online
NVPM VERB: CORE_1 /sys/devices/system/cpu/cpu1/online
NVPM VERB: CORE_2 /sys/devices/system/cpu/cpu2/online
NVPM VERB: CORE_3 /sys/devices/system/cpu/cpu3/online
NVPM VERB: CORE_4 /sys/devices/system/cpu/cpu4/online
NVPM VERB: CORE_5 /sys/devices/system/cpu/cpu5/online
NVPM VERB: CORE_6 /sys/devices/system/cpu/cpu6/online
NVPM VERB: CORE_7 /sys/devices/system/cpu/cpu7/online
NVPM VERB: CORE_8 /sys/devices/system/cpu/cpu8/online
NVPM VERB: CORE_9 /sys/devices/system/cpu/cpu9/online
NVPM VERB: CORE_10 /sys/devices/system/cpu/cpu10/online
NVPM VERB: CORE_11 /sys/devices/system/cpu/cpu11/online
NVPM VERB: PARAM TYPE=FILE NAME=TPC_POWER_GATING
NVPM VERB: TPC_PG_MASK /sys/devices/gpu.0/tpc_pg_mask
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_ENABLE
NVPM VERB: GPU_PWR_CNTL_EN /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=FILE NAME=GPU_POWER_CONTROL_DISABLE
NVPM VERB: GPU_PWR_CNTL_DIS /sys/devices/gpu.0/power/control
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_A78_0
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_A78_1
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu4/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=CPU_A78_2
NVPM VERB: FREQ_TABLE /sys/devices/system/cpu/cpu8/cpufreq/scaling_available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/system/cpu/cpu8/cpufreq/scaling_max_freq
NVPM VERB: MIN_FREQ /sys/devices/system/cpu/cpu8/cpufreq/scaling_min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=GPU
NVPM VERB: FREQ_TABLE /sys/devices/17000000.ga10b/devfreq_dev/available_frequencies
NVPM VERB: MAX_FREQ /sys/devices/17000000.ga10b/devfreq_dev/max_freq
NVPM VERB: MIN_FREQ /sys/devices/17000000.ga10b/devfreq_dev/min_freq
NVPM VERB: PARAM TYPE=CLOCK NAME=EMC
NVPM VERB: MAX_FREQ /sys/kernel/nvpmodel_emc_cap/emc_iso_cap
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA0_CORE
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/15880000.nvdla0/acm/clk_cap/dla0_core
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA0_FALCON
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/15880000.nvdla0/acm/clk_cap/dla0_falcon
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA1_CORE
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/158c0000.nvdla1/acm/clk_cap/dla1_core
NVPM VERB: PARAM TYPE=CLOCK NAME=DLA1_FALCON
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/158c0000.nvdla1/acm/clk_cap/dla1_falcon
NVPM VERB: PARAM TYPE=CLOCK NAME=PVA0_VPS
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/16000000.pva0/acm/clk_cap/pva0_vps
NVPM VERB: PARAM TYPE=CLOCK NAME=PVA0_AXI
NVPM VERB: MAX_FREQ /sys/devices/platform/13e40000.host1x/16000000.pva0/acm/clk_cap/pva0_cpu_axi
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=0 NAME=MAXN
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 1
NVPM VERB: CPU_ONLINE CORE_7 1
NVPM VERB: CPU_ONLINE CORE_8 1
NVPM VERB: CPU_ONLINE CORE_9 1
NVPM VERB: CPU_ONLINE CORE_10 1
NVPM VERB: CPU_ONLINE CORE_11 1
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_A78_0 MIN_FREQ 729600
NVPM VERB: CPU_A78_0 MAX_FREQ 2147483647
NVPM VERB: CPU_A78_1 MIN_FREQ 729600
NVPM VERB: CPU_A78_1 MAX_FREQ 2147483647
NVPM VERB: CPU_A78_2 MIN_FREQ 729600
NVPM VERB: CPU_A78_2 MAX_FREQ 2147483647
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 2147483647
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 0
NVPM VERB: DLA0_CORE MAX_FREQ 2147483647
NVPM VERB: DLA1_CORE MAX_FREQ 2147483647
NVPM VERB: DLA0_FALCON MAX_FREQ 2147483647
NVPM VERB: DLA1_FALCON MAX_FREQ 2147483647
NVPM VERB: PVA0_VPS MAX_FREQ 2147483647
NVPM VERB: PVA0_AXI MAX_FREQ 2147483647
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=1 NAME=MODE_15W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 0
NVPM VERB: CPU_ONLINE CORE_5 0
NVPM VERB: CPU_ONLINE CORE_6 0
NVPM VERB: CPU_ONLINE CORE_7 0
NVPM VERB: CPU_ONLINE CORE_8 0
NVPM VERB: CPU_ONLINE CORE_9 0
NVPM VERB: CPU_ONLINE CORE_10 0
NVPM VERB: CPU_ONLINE CORE_11 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 248
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_A78_0 MIN_FREQ 729600
NVPM VERB: CPU_A78_0 MAX_FREQ 1113600
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 420750000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 2133000000
NVPM VERB: DLA0_CORE MAX_FREQ 614400000
NVPM VERB: DLA1_CORE MAX_FREQ 614400000
NVPM VERB: DLA0_FALCON MAX_FREQ 294400000
NVPM VERB: DLA1_FALCON MAX_FREQ 294400000
NVPM VERB: PVA0_VPS MAX_FREQ 307200000
NVPM VERB: PVA0_AXI MAX_FREQ 217600000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=2 NAME=MODE_30W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 1
NVPM VERB: CPU_ONLINE CORE_7 1
NVPM VERB: CPU_ONLINE CORE_8 0
NVPM VERB: CPU_ONLINE CORE_9 0
NVPM VERB: CPU_ONLINE CORE_10 0
NVPM VERB: CPU_ONLINE CORE_11 0
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 240
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_A78_0 MIN_FREQ 729600
NVPM VERB: CPU_A78_0 MAX_FREQ 1728000
NVPM VERB: CPU_A78_1 MIN_FREQ 729600
NVPM VERB: CPU_A78_1 MAX_FREQ 1728000
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 624750000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 0
NVPM VERB: DLA0_CORE MAX_FREQ 1369600000
NVPM VERB: DLA1_CORE MAX_FREQ 1369600000
NVPM VERB: DLA0_FALCON MAX_FREQ 729600000
NVPM VERB: DLA1_FALCON MAX_FREQ 729600000
NVPM VERB: PVA0_VPS MAX_FREQ 512000000
NVPM VERB: PVA0_AXI MAX_FREQ 358400000
NVPM VERB:
NVPM VERB: POWER_MODEL: ID=3 NAME=MODE_50W
NVPM VERB: CPU_ONLINE CORE_0 1
NVPM VERB: CPU_ONLINE CORE_1 1
NVPM VERB: CPU_ONLINE CORE_2 1
NVPM VERB: CPU_ONLINE CORE_3 1
NVPM VERB: CPU_ONLINE CORE_4 1
NVPM VERB: CPU_ONLINE CORE_5 1
NVPM VERB: CPU_ONLINE CORE_6 1
NVPM VERB: CPU_ONLINE CORE_7 1
NVPM VERB: CPU_ONLINE CORE_8 1
NVPM VERB: CPU_ONLINE CORE_9 1
NVPM VERB: CPU_ONLINE CORE_10 1
NVPM VERB: CPU_ONLINE CORE_11 1
NVPM VERB: TPC_POWER_GATING TPC_PG_MASK 0
NVPM VERB: GPU_POWER_CONTROL_ENABLE GPU_PWR_CNTL_EN on
NVPM VERB: CPU_A78_0 MIN_FREQ 729600
NVPM VERB: CPU_A78_0 MAX_FREQ 1497600
NVPM VERB: CPU_A78_1 MIN_FREQ 729600
NVPM VERB: CPU_A78_1 MAX_FREQ 1497600
NVPM VERB: CPU_A78_2 MIN_FREQ 729600
NVPM VERB: CPU_A78_2 MAX_FREQ 1497600
NVPM VERB: GPU MIN_FREQ 0
NVPM VERB: GPU MAX_FREQ 828750000
NVPM VERB: GPU_POWER_CONTROL_DISABLE GPU_PWR_CNTL_DIS auto
NVPM VERB: EMC MAX_FREQ 0
NVPM VERB: DLA0_CORE MAX_FREQ 1369600000
NVPM VERB: DLA1_CORE MAX_FREQ 1369600000
NVPM VERB: DLA0_FALCON MAX_FREQ 729600000
NVPM VERB: DLA1_FALCON MAX_FREQ 729600000
NVPM VERB: PVA0_VPS MAX_FREQ 704000000
NVPM VERB: PVA0_AXI MAX_FREQ 486400000
NVPM VERB:
EOF
}

show_query()
{
    local name=$1
    local number=$2
    
    echo "NVPM WARN: fan mode is not set!"
    echo "NV Power Mode: $name"
    echo "$number"
}

set_level()
{
    local level=$1
    if [ "$EUID" -ne 0 ] ; then
        echo "Please run as root"
        exit 1
    fi
    
    if [ "$level" -eq "2" ] ; then
        echo "NVPM ERROR: I can't set this model!"
        echo "NVPM ERROR: Need a reboot"
        read -p "NVPM WARNING: Write YES" yn
        echo $yn
        elif [ "$level" -ge "0" ] && [ "$level" -lt "4" ] ; then
        echo "Save"
        echo "$level" > /tmp/nvp_model_test
    else
        echo "NVPmodel $level does not exist"
    fi
}

main()
{
    local PARSE=false
    local VERBOSE=false
    local modes=("MAXTEST" "TEST" "MINTEST" "MIN_MAX_TEST")
    local level=0
    # Load level from temp file
    if [ -f /tmp/nvp_model_test ] ; then
        level=$(cat /tmp/nvp_model_test)
    fi
    
    if [[ $# -eq 0 ]] ; then
        echo 'nvpmodel emulator'
        exit 0
    fi
    # Decode all information from startup
    while [ -n "$1" ]; do
        case "$1" in
            --verbose)
                VERBOSE=true
            ;;
            -p)
                PARSE=true
            ;;
            -m)
                level=$2
                shift 1
                set_level $level
                exit 0
            ;;
            -q)
                show_query ${modes[level]} $level
                exit 0
            ;;
            -h|--help)
                usage
                exit 0
            ;;
            *)
                usage "[ERROR] Unknown option: $1"
                exit 1
            ;;
        esac
        shift 1
    done
    
    if $PARSE ; then
        echo "NVPM VERB: Config file: /etc/nvpmodel.conf"
        echo "NVPM VERB: parsing done for /etc/nvpmodel.conf"
        echo "succeed to parse file /etc/nvpmodel.conf."
    fi
    
    if $PARSE && $VERBOSE ; then
        show_verbose_test
    fi
}

main $@
exit 0

# EOF
